home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Atari Mega Archive 1
/
Atari Mega Archive - Volume 1.iso
/
archiver
/
briksrc.zoo
/
brik.c
< prev
next >
Wrap
C/C++ Source or Header
|
1991-05-16
|
30KB
|
968 lines
/* ::[[ @(#) brik.c 1.55 89/07/12 03:31:06 ]]:: */
#ifndef LINT
static char sccsid[]="::[[ @(#) brik.c 1.55 89/07/12 03:31:06 ]]::";
#endif
#define DATESTAMP "1989/07/11"
#define VERSION "2.0"
/*
(c) Copyright 1989 Rahul Dhesi, All rights reserved. Permission is
granted to use, copy, and distribute this file under the terms of the
GNU General Public License version 1.0.
*/
/*
Checksum: 2040181157 (check or update this with "brik")
*/
static char copyright[] =
"\
Copyright 1989 Rahul Dhesi, All rights reserved. Use and distribution is\n\
permitted under the terms of the GNU General Public License version 1.0.\n\
";
/*
The following code assumes the ASCII character set and 8-bit bytes.
*/
#ifndef OK_STDIO
# include <stdio.h>
# define OK_STDIO
#endif
#include "brik.h" /* configuration options */
#include "assert.h"
typedef unsigned long tcrc; /* type of crc value -- same as in addbfcrc.c */
#ifdef GENTAB
void mkcrctab PARMS ((void));
#endif /* GENTAB */
#ifdef STDINCLUDE
# include <stdlib.h>
# include <string.h>
#else
FILE *fopen PARMS ((char *filename, char *mode));
char *nextfile PARMS ((int, char *, int));
char *strcat PARMS ((char *, char *));
char *strchr PARMS ((char *, char));
char *strcpy PARMS ((char *, char *));
char *strncat PARMS ((char *, char *, int));
int strcmp PARMS ((char *, char *));
int strlen PARMS ((char *));
void exit PARMS ((int status));
#endif /* STDINCLUDE */
/* twilight zone functions -- may or may not be standard */
int main PARMS ((int, char **));
int getopt PARMS ((int, char **, char *));
/* our own functions */
FILE *efopen PARMS ((char *filename, char *mode, int errlevel));
char suffix PARMS ((void));
char *nextfile PARMS ((int, char *, int));
void dofname PARMS ((char *));
void dofptr PARMS ((FILE *, char *));
int lowerit PARMS ((int));
void printhdr PARMS ((void));
void readnames PARMS ((FILE *));
void updatefile PARMS ((FILE *, long, tcrc, char *));
int whole_check PARMS ((FILE *, char *));
tcrc findcrc PARMS ((FILE *, char *, int *));
tcrc xatol PARMS ((char *));
void addbfcrc PARMS ((char *, int));
void brktst PARMS ((void));
void hdrcrc PARMS ((FILE *, char *));
void longhelp PARMS ((void));
void shorthelp PARMS ((void));
void showerr PARMS ((char *, int));
/* the following constants can be changed if you know what you are doing */
#define ERRLIMIT 127 /* exit(n) returns n not exceeding this */
#define LINESIZE 1024 /* handle lines of up to this length */
#define ERRBUFSIZ 1024 /* buffer size for perror() message */
#define BACKSIZE 1024 /* how much to seek back looking for header */
#define BINTABSIZ 256 /* size of binary char test table */
#ifdef CTRLZ_CHECK
# define Z_THRESHOLD 10 /* see how CTRLZ_CHECK is used later */
#endif /* CTRLZ_CHECK */
/* the following constants should not be changed */
#define MYNL 10 /* newline for CRC calculation */
#define PATTERN "Checksum:" /* look for this header */
#define CHARSINCRC 10 /* characters in CRC */
#define CMTCH '#' /* begins comment in CRC list */
/* error levels */
#define LVL_WARN 0
#define LVL_ERR 1
#define LVL_FATAL 2
#ifdef USEINDEX
# define strchr index
#endif /* USEINDEX */
#ifdef NOTRAIL_B /* avoid trailing "b" in mode string */
# define BRIK_RD "r"
# define BRIK_RW "r+"
# define BRIK_RDB "r"
#else
# define BRIK_RD "r"
# define BRIK_RW "r+"
# define BRIK_RDB "rb"
#endif /* NOTRAIL_B */
#define whitespace(x) (strchr(" \t\n",(x)) != NULL)
/* format strings for printing CRCs and filenames etc. */
static char ok[] = "ok ";
static char bad[] = "BAD";
static char blank[] = " ";
static char fmtstr[] = "%10lu%c %s %s\n";
static char hdrfmt[] = "Checksum: %10lu %s\n";
static char version[] = VERSION;
char bintab[BINTABSIZ]; /* binary char test table */
int patlen; /* length of PATTERN */
int errcount = 0; /* count of errors */
int gen1 = 0; /* generate CRCs for all files */
int gen2 = 0; /* generate CRCs for files with headers */
int silent = 0; /* be silent, just set error status */
int quiet = 0; /* talks less, but not completely silent */
int verbose = 0; /* be verbose, print message for good files too */
int updfile = 0; /* update file by inserting CRC */
int check1 = 0; /* whether to check header crcs */
int check2 = 0; /* whether to check whole file crcs */
int fromfile = 0; /* read filenames from a file */
int binary = 0; /* manipulate binary file */
int trailing = 0; /* include trailing empty lines */
int prthdr = 0; /* print Checksum: XXXXXXXXXX header */
int autocheck = 0; /* brik must decide if text or binary */
int is_stdin = 0; /* current file is stdin */
int doubtful = 0; /* text but doubtful */
#ifdef DEBUG
int debugging = 0;
#endif
/* opens file, prints error message if can't open */
FILE *efopen (fname, mode, level)
char *fname; /* filename to open */
char *mode; /* mode, e.g. "r" or "r+" */
int level; /* error level */
{
FILE *fptr;
fptr = fopen (fname, mode);
if (fptr == NULL)
showerr (fname, level);
return (fptr);
}
/* LOWERIT is a function or macro that returns lowercase of a character */
#ifndef LOWERIT
# ifdef AVOID_MACROS
# define LOWERIT lowerit
# else
# define LOWERIT(c) ((c)>='A' && (c)<='Z' ? ('a'-'A')+(c) : (c))
# endif
#endif
/* Function needed by SEEKFIX code even if a macro is available */
int lowerit (c) int c; /* returns lowercase of an ASCII character */
{
if (c >= 'A' && c <= 'Z') return (('a'-'A') + c);
else return (c);
}
/* STRNICMP is a case-insensitive strncmp */
#ifndef STRNICMP
int STRNICMP (s1, s2, n)
register char *s1, *s2;
int n;
{
assert (n >= 0);
assert (LOWERIT('X') == 'x');
assert (LOWERIT('*') == '*');
for ( ; LOWERIT(*s1) == LOWERIT(*s2); s1++, s2++) {
if (--n == 0 || *s1 == '\0')
return(0);
}
return(LOWERIT(*s1) - LOWERIT(*s2));
}
#endif /* STRNICMP */
#ifdef AVOID_MACROS
# define BRINCMP STRNICMP
#else
# define BRINCMP(s1,s2,n) (LOWERIT(*(s1))!=LOWERIT(*(s2))||STRNICMP(s1,s2,n))
#endif
#define xdigit(x) ((x) >= '0' && (x) <= '9')
/*
xatol is given a string that (supposedly) begins with a string
of digits. It returns a corresponding positive numeric value.
*/
tcrc xatol (str)
char *str;
{
tcrc retval;
retval = 0L;
while (xdigit(*str)) {
retval = retval * 10L + (*str-'0');
str++;
}
return (retval);
}
main (argc, argv)
int argc;
char **argv;
{
int i;
int c; /* next option letter */
int count = 0; /* count of required options seen */
char *infname; /* name of file to read filenames from */
FILE *infptr; /* open file ptr for infname */
extern int optind; /* from getopt: next arg to process */
extern int opterr; /* used by getopt */
opterr = 1; /* so getopt will print err msg */
argv[0] = "brik"; /* for getopt to use */
patlen = strlen (PATTERN);
#ifdef DEBUG
while ((c = getopt (argc, argv, "cCgGasqvWHfbThd")) != EOF)
#else
while ((c = getopt (argc, argv, "cCgGasqvWHfbTh")) != EOF)
#endif
{
switch (c) {
case 'a': autocheck++; binary = 0; trailing = 0; break;
case 'c': check1++; count++; break;
case 'C': check2++; count++; break;
case 'g': gen1++; count++; break;
case 'G': gen2++; count++; break;
case 's': silent++; verbose = 0; break;
case 'q': quiet++; verbose = 0; break;
case 'v': verbose++; silent = 0; break;
case 'W': updfile++; break;
case 'f': fromfile++; break;
case 'b': binary++; autocheck = 0; trailing = 0; break;
case 'T': trailing++; binary = 0; autocheck = 0; break;
case 'H': prthdr++; break;
#ifdef DEBUG
case 'd': debugging++; break;
#endif
case 'h': longhelp();
case '?':